package com.zlx.sharelive.activity;

import android.media.MediaPlayer;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.Toast;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.sharelive.R;
import com.zlx.sharelive.view.CustomVideoView;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 短视频预览
 */
public class ShortPreviewAc extends BaseAc {

    @BindView(R.id.videoView)
    CustomVideoView videoView;
    @BindView(R.id.iv_play)
    ImageView iv_play;

    private String path;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_short_preview;
    }


    @Override
    public void initViews() {
        super.initViews();

        path = getIntent().getStringExtra("path");

        videoView.setVideoPath(path);

        //创建MediaController对象
        MediaController mediaController = new MediaController(this);

        //VideoView与MediaController建立关联
        videoView.setMediaController(mediaController);

        //让VideoView获取焦点
        videoView.requestFocus();

        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                //your code
                iv_play.setVisibility(View.VISIBLE);
            }
        });
    }


    @OnClick({R.id.iv_play, R.id.bt_comm})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_play:
                playVideo(path);
                iv_play.setVisibility(View.GONE);
                break;
            case R.id.bt_comm:
                finish();
                break;
        }
    }

    private void playVideo(String path) {
        File file = new File(path);
        if (!file.exists()) {
            Toast.makeText(this, "视频文件路径错误", Toast.LENGTH_SHORT).show();
        }

        videoView.setVideoPath(file.getAbsolutePath());
        videoView.start();


    }


}
